<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="1200px" append-to-body>
        <el-form :model="queryParams" ref="queryForm" inline label-width="80px">
                <el-form-item label="时间" prop="customDate">
                    <el-radio-group v-model="queryParams.customDate" @change="changeCustomDate">
                        <el-radio-button :label="1">7天</el-radio-button>
                        <el-radio-button :label="2">15天</el-radio-button>
                        <el-radio-button :label="3">30天</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="日期" prop="date">
                    <el-date-picker v-model="queryParams.date" format="yyyy-MM-dd" type="daterange" placeholder="请选择日期" style="width: 240px"></el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="getDetail">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        <el-row type="flex">
            <el-col :span="24">
                <SpuSalesRanking idName="spuSalesRankingChat" :chartData="chatList" :height="480" label="quantity" />
            </el-col>
        </el-row>
    </el-dialog>
</template>

<script>
import { getSpuSalesRanking } from '@/api/report'
import SpuSalesRanking from './SpuSalesRanking'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'SpuSalesRankingDialog',
    components: {
        SpuSalesRanking,
    },
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            chatList: [],
            queryParams: {
                customDate: 3,
                date: [],
                spu: ''
            }
        }
    },
    computed: {
        title() {
            return `spu销量排行`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val) {
                    this.queryParams = Object.assign({}, this.queryParams, val)
                }
            },
            immediate: true,
            deep: true,
        },
        'queryParams.date': {
            handler(val) {
                if (val?.length == 2) {
                    this.queryParams.customDate = 0
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted() {
        this.queryParams.customDate = 3
        this.getDetail()
    },
    methods: {
        changeCustomDate() {
            this.queryParams.date = []
            this.getDetail()
        },
        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')
            this.getDetail()
        },
        getDetail() {
            this.loading = true
            const qyparams = { ...this.queryParams }
            if(qyparams.date.length==2) {
                qyparams.date = [parseTime(qyparams.date[0], 'y-m-d'), parseTime(qyparams.date[1], 'y-m-d')]; 
            }
            getSpuSalesRanking(qyparams)
                .then(res => {
                    this.chatList = res.data ?? []
                })
                .finally(() => {
                    this.loading = false
                })
        },
    },
}
</script>

<style></style>
